package com.zhike.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhike.domain.DwsTripWindow;
import com.zhike.service.DwsTripWindowService;
import com.zhike.mapper.DwsTripWindowMapper;
import com.zhike.utils.Result;
import com.zhike.utils.ResultCodeEnum;
import com.zhike.utils.BarUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
* @author ASUS
* @description 针对表【dws_trip_window(车辆行驶表)】的数据库操作Service实现
* @createDate 2024-04-30 13:57:36
*/
@Service
public class DwsTripWindowServiceImpl extends ServiceImpl<DwsTripWindowMapper, DwsTripWindow>
    implements DwsTripWindowService{

    @Autowired
    private DwsTripWindowMapper dwsTripWindowMapper;
    @Override
    public String getAvgSpeedToday(String filter) {
        String date = BarUtils.getDate(filter);
        try {
            List<Map> avgSpeedToday = dwsTripWindowMapper.getAvgSpeedToday(date);
            Map data = BarUtils.getDetail(avgSpeedToday,"vin","avg_speed","mileage","平均速度","行驶里程");
            return Result.ok(JSON.toJSONString(data)).toString();
        } catch (NumberFormatException e) {
            return Result.build(null, ResultCodeEnum.FAIL).toString();
        }


    }

    @Override
    public String getMileageToday(String filter) {
        String date = BarUtils.getDate(filter);
        try {
            List<Map> mileageToday = dwsTripWindowMapper.getMileageToday(date);
            Map data = BarUtils.getDetail(mileageToday,"vin","avg_speed","mileage","平均速度","行驶里程");
            return Result.ok(JSON.toJSONString(data)).toString();
        } catch (NumberFormatException e) {
            return Result.build(null, ResultCodeEnum.FAIL).toString();
        }
    }

    @Override
    public String getNumToday(String filter) {
        String date = BarUtils.getDate(filter);
        try {
            List<Map> numToday = dwsTripWindowMapper.getNumToday(date);
            Map data = BarUtils.getDetailSingle(numToday,"vin","danger_count","急加减次数");
            return Result.ok(JSON.toJSONString(data)).toString();
        } catch (NumberFormatException e) {
            return Result.build(null, ResultCodeEnum.FAIL).toString();
        }

    }

    @Override
    public String getAvgSpeedWeek(String filter) {
        String date = BarUtils.getDate(filter);
        try {
            List<Map> avgSpeedToday = dwsTripWindowMapper.getAvgSpeedWeek(date);
            Map data = BarUtils.getDetail(avgSpeedToday,"vin","avg_speed","mileage","平均速度","日均里程");
            return Result.ok(JSON.toJSONString(data)).toString();
        } catch (NumberFormatException e) {
            return Result.build(null, ResultCodeEnum.FAIL).toString();
        }
    }

    @Override
    public String getMileageWeek(String filter) {
        String date = BarUtils.getDate(filter);
        try {
            List<Map> mileageToday = dwsTripWindowMapper.getMileageWeek(date);
            Map data = BarUtils.getDetail(mileageToday,"vin","avg_speed","mileage","平均速度","日均里程");
            return Result.ok(JSON.toJSONString(data)).toString();
        } catch (NumberFormatException e) {
            return Result.build(null, ResultCodeEnum.FAIL).toString();
        }
    }

    @Override
    public String getNumWeek(String filter) {
        String date = BarUtils.getDate(filter);
        try {
            List<Map> numToday = dwsTripWindowMapper.getNumWeek(date);
            Map data = BarUtils.getDetailSingle(numToday,"vin","danger_count","急加减次数");
            return Result.ok(JSON.toJSONString(data)).toString();
        } catch (NumberFormatException e) {
            return Result.build(null, ResultCodeEnum.FAIL).toString();
        }
    }


}




